$(function () {
    $("#jqGrid").Grid({
        url: '../station/list',
        colModel: [{
            label: 'id', name: 'id', index: 'id', key: true, hidden: true
        }, {
            label: '区域', name: 'regionname', index: 'regionname',align:'center', width: 40
        }, {
            label: '详细地址', name: 'addrinfo', index: 'addrinfo', align:'center', width: 100
        }, {
            label: '服务站名', name: 'stationName', index: 'station_name', align:'center', width: 40
        }, {
            label: '管理员', name: 'manage', index: 'manage', align:'center', width: 20
        }, {
            label: '管理员手机', name: 'mobile', index: 'mobile',align:'center', width: 30
        }, {
            label: '落地日期', name: 'createTime', index: 'create_time',align:'center', width: 50, formatter: function (value) {
                return transDate(value);
            }
        }, {
            label: '运营日期', name: 'useTime', index: 'use_time',align:'center', width: 50, formatter: function (value) {
                return transDate(value);
            }
        }, {
            label: '状态', name: 'status', index: 'status', align:'center',width: 20, formatter: function (value) {
                return transStationStatus(value);
            }
        }]
    });
    var newHeight = $(window).height()-130;
    $(".ui-jqgrid .ui-jqgrid-bdiv").css("cssText","height: "+newHeight+"px!important;");
});

var vm = new Vue({
    el: '#station',
    data: {
        showList: true,
        title: null,
        station: {
            status: '0'
        },
        ruleValidate: {
            regionId: [
                {required: true, message: '区域不能为空'}
            ],
            addrinfo: [
                {required: true, message: '请填写详细地址', trigger: 'blur'}
            ],
            stationName: [
                {required: true, message: '请填写服务站名', trigger: 'blur'}
            ],
            manage: [
                {required: true, message: '管理员不能为空', trigger: 'blur'}
            ],
            mobile: [
                {required: true, message: '管理员手机不能为空', trigger: 'blur'},
                {pattern: /^1[3|4|5|8][0-9]\d{4,8}$/, message: '请填入正确的电话号码', trigger: 'blur'}
            ],
            createTime: [
                {required: true, message: '落地日期不能为空'}
            ]
        },
        q: {
            stationname: '',
            manage:'',
            mobile:'',
            addrinfo:'',
        },
        regions :[]
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.station = {status: '0'};
            this.loadRegion();
        },
        update: function (event) {
            var that = this;
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
            setTimeout(function(){
                that.loadRegionById(vm.station.regionId[0])
            },1000)
        },
        saveOrUpdate: function (event) {
            var url = vm.station.id == null ? "../station/save" : "../station/update";
            vm.station.regionId = vm.station.regionId[(vm.station.regionId.length-1)];
            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.station),
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });

        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    type: "POST",
                    url: "../station/delete",
                    contentType: "application/json",
                    params: JSON.stringify(ids),
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        exportStation: function () {
            exportFile('#station', '../station/export', {'username': vm.q.username});
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../station/info/" + id,
                async: true,
                successCallback: function (r) {
                    if( r.code == 0 ){
                        vm.station = r.station;
                        var array = [];
                        array.push(r.station.regionId)
                        vm.station.regionId = array;
                    }
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'stationName': vm.q.stationname,
                    'manage': vm.q.manage,
                    'mobile': vm.q.mobile,
                    'addrinfo': vm.q.addrinfo
                },
                page: page,
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
            vm.station.regionId = [];
        },
        /**
         * 加载区域
         */
        loadRegion : function (item, callback) {
            var parentId = "1";
            if(item != null && item != ''){
                item.loading = true;
                parentId = item.value;
            }
            Ajax.request({
                url: "../sys/region/loadRegion/" + parentId,
                async: true,
                successCallback: function (r) {
                    if (r.code == 0) {
                        if(item != null && item != ''){
                            if( r.regions.length == 0 ){
                                delete item.loading;
                                delete item.children;
                            }else{
                                item.children = r.regions;
                                item.loading = false;
                                callback();
                            }
                        } else {
                            vm.regions = r.regions;
                        }
                    }
                }
            });
        },
        /**
         * 根据id加载区域
         */
        loadRegionById : function (id) {
            Ajax.request({
                url: "../sys/region/loadRegionById/" + id,
                async: true,
                successCallback: function (r) {
                    if ( r.code == 0 ) {
                        vm.regions = r.result.regions;
                        vm.station.regionId = r.result.regionId;
                        vm.station.regionId.sort();
                    }
                }
            });
        }
    }
});
